package com.wiz.web.controllor;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

import javax.annotation.Resource;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.reflect.TypeToken;
import com.wiz.web.service.FuellingService;
import com.wiz.web.vo.criteria.FuellingOrderCriteria;
import com.wiz.web.vo.criteria.ListCriteria;

@SuppressWarnings("all")
@Controller
@RequestMapping("/fuelling")
public class FuellingControllor extends BaseControllor{
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource
	private FuellingService fuellingService;

	/**
	 * 订单上传
	 * @param request
	 * @param params
	 * @param stationId
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/ordinger", method = RequestMethod.POST)
	@ResponseBody
	public String insertOrder(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String order = null;
		try{
			order = getJsonStrByParam(stationId, params);
			ListCriteria<FuellingOrderCriteria> listCriteria = ListCriteria.fromJson(order, FuellingOrderCriteria.class);
			List<FuellingOrderCriteria> criteriaList = listCriteria.getDataList();
			return fuellingService.syncOrders(criteriaList);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("syncOrders error in "+stationId+"  params="+order+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	
	/**
	 * 根据条件获取订单信息
	 */
	@RequestMapping(value = "/order/get", method = RequestMethod.POST)
	@ResponseBody
	public String getOrders(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String order = null;
		try{
			order = getJsonStrByParam(stationId, params);
			FuellingOrderCriteria criteria = gson.fromJson(order, new TypeToken<FuellingOrderCriteria>(){}.getType());
			criteria.setStationId(stationId);
			return fuellingService.getOrders(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("getOrders error in "+stationId+"  params="+order+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
}
